var mapitto = new Object();

mapitto.Methods = {
  removeChildren: function(el) {
    el = $(el);
    el = $A(el.childNodes);
    el.each( function(node) {
      Element.remove(node);
    });
  },

  scale: function(length, scale) {
    unit = this.getUnit(length);
    length = parseInt(length);
    length *= scale;

    return length + unit;
  },

  lengthDifference: function(len1, len2) {
    len1unit = this.getUnit(this.fixUnit(len1));
    len2unit = this.getUnit(this.fixUnit(len2));

    if(len1unit != len2unit){
      alert('lengths do not match');
      return false;
    }

    len1 = parseInt(len1);
    len2 = parseInt(len2);

    len = len1 > len2 ? len1 - len2 : len2 - len1;
    return len + len1unit;
  },

  fixUnit: function(length) {
    if(this.getUnit(length) == false){
      length += "px";
    }

    return length;
  },

  getUnit: function(length) {
    if(length.match(/[0-9]em$/)){
      return "em";
    } else if(length.match(/[0-9]px$/)){
      return "px";
    } else if(length.match(/[0-9]%$/)){
      return "%";
    } else {
      return false;
    }
  },

  getLengthUnit: function(length) {
    return this.getUnit(length);
  },

  getWidth: function(el) {
    return Element.getStyle($(el), "width");
  },

  getWidthUnit: function(el) {
    return this.getLengthUnit(this.getWidth(el));
  },

  setWidth: function(el, length) {
  //  style = new Object(); 
  //  style.width = width;
    ori_unit = this.getWidthUnit(el);
    final_unit = this.getLengthUnit(length);
    if(ori_unit == final_unit) {
      Element.setStyle($(el), {width: length});
    } else {
      alert('mapitto.setWidth: units do not match');
    }
  },

  setWidthForce: function(el, length) {
    if(this.getLengthUnit(length) == false){
      length += "px";
    }

    Element.setStyle($(el), {width: length});
  },

  adjustWidth: function(el, length) {
    ori_unit = this.getWidthUnit(el);
    final_unit = this.getLengthUnit(length);

    if(ori_unit == final_unit) {
      Element.setStyle($(el), {width: length});
    } else {
      alert('mapitto.adjustWidth: units do not match');
    }
  },

  /**
   * Height
   */
  getHeight: function(el) {
    return Element.getStyle($(el), "height");
  },

  getHeightUnit: function(el) {
    return this.getLengthUnit(this.getHeight(el));
  },

  setHeight: function(el, length) {
  //  style = new Object(); 
  //  style.height = height;
    ori_unit = this.getHeightUnit(el);
    final_unit = this.getLengthUnit(length);
    if(ori_unit == final_unit) {
      Element.setStyle($(el), {height: length});
    } else {
      alert('mapitto.setHeight: units do not match');
    }
  },

  setHeightForce: function(el, length) {
    Element.setStyle($(el), {height: length});
  },

  adjustHeight: function(el, length) {
    ori_unit = this.getHeightUnit(el);
    final_unit = this.getLengthUnit(length);

    if(ori_unit == final_unit) {
      Element.setStyle($(el), {height: length});
    } else {
      alert('mapitto.adjustHeight: units do not match');
    }
  }
};

Object.extend(mapitto, mapitto.Methods);

function mapWidth() {
  width = mapitto.getWidth('header');
  unit = mapitto.getUnit(mapitto.fixUnit(width));
  width = parseInt(width);

  if(width < 600){
    width = 600;
  }

  return width + unit;
};

function mapHeight() {
  height = mapitto.lengthDifference(
  mapitto.scale(mapitto.getHeight('header'), 4),
  window.innerHeight.toString());

  unit = mapitto.getUnit(height);
  height = parseInt(height);

  if(height < 375){
    height = 375;
  }

  return height + unit;
};

